package com.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.wx.model.UserModel;
import com.wx.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    IUserService IUserService;

    @PostMapping("/exit")
    public int exit(HttpSession session){
        session.setAttribute("username",null);
      return 1;
    }

    @GetMapping("/getUsername")
    public JSONObject getUsername(HttpSession session){
        String username= (String) session.getAttribute("username");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("username",username);
        return jsonObject;
    }

    @PostMapping("/addUser")
    public JSONObject addUser(@RequestBody UserModel userModel){
        int result=IUserService.addUser(userModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("result",result);
        return jsonObject;
    }

    @PostMapping("/delete")
    public JSONObject delete(@RequestBody UserModel userModel){
        int result=IUserService.delete(userModel.getId());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("result",result);
        return jsonObject;
    }

    @PostMapping("/finById")
    public JSONObject finById(@RequestBody UserModel userModel){
        UserModel user=IUserService.finById(userModel.getId());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("userModel",user);
        return jsonObject;
    }

    @PostMapping("/update")
    public JSONObject update(@RequestBody UserModel userModel){
        int result=IUserService.update(userModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("result",result);
        return jsonObject;
    }

    @PostMapping("/conditionFind")
    public JSONObject conditionFind(@RequestBody UserModel userModel){
        userModel.setPageNum(userModel.getPageNum()==null || "".equals(userModel.getPageNum())? 1 :(userModel.getPageNum()));
        userModel.setPageSize(userModel.getPageSize()==null || "".equals(userModel.getPageSize()) ? 10:(userModel.getPageSize()));
        userModel.setStartBrithday(userModel.getStartBrithday()==null?"":userModel.getStartBrithday());
        userModel.setEndBrithday(userModel.getEndBrithday()==null?"":userModel.getEndBrithday());
        String hobby=userModel.getHobby();
        hobby=hobby.replace("[","")
                .replace("]","")
                .replaceAll(" ","")
                .replaceAll("\"","");
        userModel.setHobby(hobby);
        String hobbys[]=hobby.split(",");
        List<String> hobbyList=new ArrayList<>();
        hobbyList.addAll(Arrays.asList(hobbys));
        userModel.setHobbyList(hobbyList);
        List<UserModel> user=IUserService.conditionFind(userModel);
        int number=IUserService.getCount(userModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("user",user);
        jsonObject.put("number",number);
        return jsonObject;
    }
}
